package org.rsbot.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class ScriptClassLoader extends ClassLoader
{
  private final URL base;

  public ScriptClassLoader(URL paramURL)
  {
    this.base = paramURL;
  }

  public Class<?> loadClass(String paramString, boolean paramBoolean) throws ClassNotFoundException {
    Class localClass = findLoadedClass(paramString);

    if (localClass == null) {
      try {
        InputStream localInputStream = getResourceAsStream(paramString.replace('.', '/') + ".class");
        byte[] arrayOfByte1 = new byte[4096];
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        int i;
        while ((i = localInputStream.read(arrayOfByte1, 0, 4096)) != -1) {
          localByteArrayOutputStream.write(arrayOfByte1, 0, i);
        }
        byte[] arrayOfByte2 = localByteArrayOutputStream.toByteArray();
        localClass = defineClass(paramString, arrayOfByte2, 0, arrayOfByte2.length);
        if (paramBoolean)
          resolveClass(localClass);
      }
      catch (Exception localException) {
        localClass = super.loadClass(paramString, paramBoolean);
      }
    }

    return localClass;
  }

  public URL getResource(String paramString) {
    try {
      return new URL(this.base, paramString); } catch (MalformedURLException localMalformedURLException) {
    }
    return null;
  }

  public InputStream getResourceAsStream(String paramString)
  {
    try {
      return new URL(this.base, paramString).openStream(); } catch (IOException localIOException) {
    }
    return null;
  }
}